package com.ftg.learn.chapter08;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 案例
 *
 * @author 布衣
 */
public class JdbcDemo01<E> {

    private final String userName = "root";
    private final String passWord = "123456";
    private final String url = "jdbc:mysql://localhost:3306/exam?serverTimezone=Asia/Harbin";

    /**
     * 查询
     *
     * @param sql    语句
     * @param params 参数
     * @return List 结果 集
     */
    public List<Map<String, Object>> query(String sql, Object... params) {
        List<Map<String, Object>> list = new ArrayList<>();
        //会话
        Connection connection = null;

        //预编译语句的掊口
        PreparedStatement statement = null;

        //结果集
        ResultSet resultSet = null;
        try {
//            //加载驱动
//            Class.forName("com.mysql.cj.jdbc.Driver");
//
//            //DriverManager 驱动管理
//            //注册
//            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());

            //会话得到了
            connection = DriverManager.getConnection(url, userName, passWord);
            statement = connection.prepareStatement(sql);
            //预编译sql语句，里的?赋值
            for (int i = 0; i < params.length; i++) {
                statement.setObject(i + 1, params[i]);
            }

            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Map<String, Object> map = new HashMap<>();
                //jdbc 结果集处理类
                ResultSetMetaData metaData = resultSet.getMetaData();
                for (int i = 0; i < metaData.getColumnCount(); i++) {
                    map.put(metaData.getColumnName(i + 1).toLowerCase(), resultSet.getObject(i + 1));
                }
                list.add(map);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return list;
    }

    /**
     * 反射机制 : 将查询结果集封装成对象
     * 1 : 查看反射
     * 2 ： 尝试反射解决我们map转换成pojo问题
     * 3 : 能不能给你一个表名，全自动生成所有的select insert update delete
     * 4 : 是否可以把3起一名字呢？ mybatis 可以吗？
     * @param sql
     * @param params
     * @return
     */
    public List<E> queryObject(String sql, Object... params) {
        List<E> list = new ArrayList<>();
        //会话
        Connection connection = null;

        //预编译语句的掊口
        PreparedStatement statement = null;

        //结果集
        ResultSet resultSet = null;
        try {
//            //加载驱动
//            Class.forName("com.mysql.cj.jdbc.Driver");
//
//            //DriverManager 驱动管理
//            //注册
//            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());

            //会话得到了
            connection = DriverManager.getConnection(url, userName, passWord);
            statement = connection.prepareStatement(sql);
            //预编译sql语句，里的?赋值
            for (int i = 0; i < params.length; i++) {
                statement.setObject(i + 1, params[i]);
            }

            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Map<String, Object> map = new HashMap<>();
                //jdbc 结果集处理类
                ResultSetMetaData metaData = resultSet.getMetaData();
                for (int i = 0; i < metaData.getColumnCount(); i++) {
                    map.put(metaData.getColumnName(i + 1).toLowerCase(), resultSet.getObject(i + 1));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return list;
    }

    /**
     * 更新
     *
     * @param sql    语句
     * @param params 参数
     * @return 影响行数
     */
    public int update(String sql, Object... params) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            //会话得到了
            connection = DriverManager.getConnection(url, userName, passWord);
            statement = connection.prepareStatement(sql);
            //预编译sql语句，里的?赋值
            for (int i = 0; i < params.length; i++) {
                statement.setObject(i + 1, params[i]);
            }
            return statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return -1;
    }


    public static void main(String[] args) {

        JdbcDemo01 jdbcDemo01 = new JdbcDemo01();
        List<Map<String, Object>> query = jdbcDemo01.query("select * from students where name like concat(?,'%')", "A");
        System.out.println(query);

    }

}
